package com.gome.oa.listener;

import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.event.NavigationAdapter;

/**
 *
 * @author: tang li
 * @date: 2016年8月13日
 * @desc: 
 *
 */
public class MainNavigationAdapter extends NavigationAdapter {
	
	private JTextField txtAddressBar;
	
	public MainNavigationAdapter(JTextField txtAddressBar) {
		this.txtAddressBar = txtAddressBar;
	}
	
	public void navigationCompleted(final Browser browser, final String url) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				txtAddressBar.setText(browser.getLocationURL());
				txtAddressBar.setCaretPosition(0);
			}
		});
	}

	public void frameDocumentCompleted(final Browser browser, final String url) {
		if(url.contains("/howebapp/gomeoaportal/frames/navBar1.aspx")) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					/*browser.executeScript("document.frames['navBar'].document.getElementById('DataList1_ctl01_formUrl').innerHTML='OA加班申请单(非原版)';");
			    	browser.executeScript("var click = document.frames['navBar'].document.getElementById('DataList1_ctl01_formUrl').getAttribute('onclick').toString();"
			    			+ "var start = click.indexOf('http');"
			    			+ "var length = click.indexOf(',') - start;"
			    			+ "var clickUrl = click.substr(start, length-1);");
			    			
			    	JSValue returnValue = browser.executeScript("clickUrl");
			    	browser.executeScript("document.frames['navBar'].document.getElementById('DataList1_ctl01_formUrl').onclick='javascript:void(0)';");
			    	browser.executeScript("document.frames['navBar'].document.getElementById('DataList1_ctl01_formUrl').target='_blank';");
			    	browser.executeScript("document.frames['navBar'].document.getElementById('DataList1_ctl01_formUrl').href='"+returnValue.getString()+"';");*/
					
					String script = "var links = document.frames['navBar'].document.getElementById('DataList1').getElementsByTagName('a');" + 
							"for(var i=0; i<links.length;i++){" + 
							"	var link = links[i];" + 
							"	if(link.innerHTML == 'OA加班申请单') {" + 
							"		 link.innerHTML = 'OA加班申请单(非原版)';" + 
							"		 var click = link.getAttribute('onclick').toString();" + 
							"		 var start = click.indexOf('http');" + 
							"		 var length = click.indexOf(',') - start;" + 
							"		 var clickUrl = click.substr(start, length-1);" + 
							"		 link.onclick='javascript:void(0)';" + 
							"		 link.target='_blank';" + 
							"		 link.href=clickUrl;" + 
							"	}" + 
							"}";
					browser.executeScript(script);
					
					
				}
			});
		}
	}
	
	public void mainDocumentCompleted(final Browser browser, final String url) {
		
	}
}
